# -*- coding: utf-8 -*-
from django.db import models

# Create your models here.

# felix's work

class Knowledge(models.Model):
	"""
	This class is an abstract class and has several common fields.
	
	1. name
	2. explanation
	3. is_raised   -> This boolean field checks whether this knowledge has set exam questions or not.
	"""
	name = models.CharField(max_length = 20, null=False)
	explanation = models.TextField(blank=False)
	is_raised = models.BooleanField(default=False)
	pub_date = models.DateTimeField('date publisehd')
	
	def __unicode__(self):
		return self.name
	
	class Meta:
		abstract = True

class Commonsense(Knowledge):
	"""
	This Commonsense class will be derived from Knowledge class and has several additional fields.

	1. full_version	-> if the commonsense is a kind of abbreviation, 
			full_vsersion would be not blanked and be filled with full sentence.
	2. category
	"""
	CATEGORY = (
                (0, u'경영/경제상식'),
                (1, u'일반상식'),
                (2, u'과학상식'),
	)

	full_version = models.TextField(blank = True)
	category = models.PositiveSmallIntegerField(null=True, choices=CATEGORY)


class Character(models.Model):
	"""
	This Character will be a part of Chineseidiom class and has several additional fields.

	1. ideogram	-> a shape of Chinese
	2. meaning
	3. sound
	
	"""
	ideogram = models.CharField(max_length=1, null=False)
	meaning = models.CharField(max_length=10, null=False)
	sound = models.CharField(max_length=1, null=False)

	def __unicode__(self):
		return self.ideogram

class Chineseidiom(Knowledge):
	"""
	This Chineseidiom class will be derived from Knowledge class and has an additional field that consists of some Characters.

	"""
	characters = models.ManyToManyField(Character, through='IdiomBroker')

class IdiomBroker(models.Model):
	"""
	This IdiomBroker class is a reference class for (Chineseidom has many Character - relation)

	#### Example

	>> character1 = Character(......)
	>> character2 = Character(......)
	>> c_idiom = Chineseidiom(.......)

	>> i_broker = IdiomBroker(character = character1, chineseidiom = c_idiom)
	>> i_broker.save()

	>> c_idiom.members.all()
	  [<Character: 言>]
	>> character1.group_set.all()
	  [<Chineseidiom: 언중유골>]
		# create_and_??? will be better
	>> i_broker = IdiomBroker(character = character2, chineseidiom = c_idiom)
	>> c_idiom.members.all()
	  [<Character: 言>, <Character: 中>]        

	"""

	character = models.ForeignKey(Character)
	chineseidiom = models.ForeignKey(Chineseidiom)
   
  def __unicode__(self):
		return u"%s (%s)" % (self.character, self.chineseidiom)


